package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.login.RequiresLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.AccountDeductVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account")
public class UserAccountApiController {

    private final UserAccountService userAccountService;

    public UserAccountApiController(UserAccountService userAccountService) {
        this.userAccountService = userAccountService;
    }

    @Operation(summary = "获取用户账户可用余额")
    @RequiresLogin(required = false)
    @GetMapping("/userAccount/getAvailableAmount")
    public Result<Double> getAvailableAmount() {
        Long userId = AuthContextHolder.getUserId();
        if (userId == null) {
            return Result.ok(0D);
        }
        return Result.ok(userAccountService.getAvailableAmountByUserId(userId));
    }

    @Operation(summary = "检查及扣减账户余额")
    @RequiresLogin
    @PostMapping("/userAccount/checkAndDeduct")
    public Result<Void> checkAndDeduct(@RequestBody AccountDeductVo accountDeductVo) {
        // 1.从ThreadLocal中获取用户ID
        Long userId = AuthContextHolder.getUserId();
        if (userId != null) {
            accountDeductVo.setUserId(userId);
        }
        // 2.调用业务逻辑完成账户余额扣款
        this.userAccountService.checkAndDeduct(accountDeductVo);
        return Result.ok();
    }


    @Operation(summary = "支付成功后更新账户")
    @Parameters({
            @Parameter(name = "orderNo", description = "订单号", required = true)
    })
    @GetMapping("/userAccount/updateAccountOnPaySuccess/{orderNo}")
    public Result<Void> updateAccountOnPaySuccess(@PathVariable String orderNo) {
        this.userAccountService.updateAccountOnPaySuccess(orderNo);
        return Result.ok();
    }

    @Operation(summary = "分页查询当前登录用户的充值记录")
    @Parameters({
            @Parameter(name = "page", description = "当前页码", required = true),
            @Parameter(name = "limit", description = "每页记录数", required = true)
    })
    @RequiresLogin
    @GetMapping("/userAccount/findUserRechargePage/{page}/{limit}")
    public Result<Page<UserAccountDetail>> findUserRechargePage(@PathVariable Integer page,
                                                                @PathVariable Integer limit) {
        Long userId = AuthContextHolder.getUserId();
        Page<UserAccountDetail> detailPage = new Page<>(page, limit);
        String tradeType = SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT;
        detailPage = this.userAccountService.getUserAccountDetailPage(detailPage, userId, tradeType);
        return Result.ok(detailPage);
    }

    @Operation(summary = "分页查询当前用户消费记录")
    @RequiresLogin
    @GetMapping("/userAccount/findUserConsumePage/{page}/{limit}")
    public Result<Page<UserAccountDetail>> getUserConsumePage(@PathVariable int page, @PathVariable int limit) {
        Long userId = AuthContextHolder.getUserId();
        Page<UserAccountDetail> consumePage = new Page<>(page, limit);
        String tradeType = SystemConstant.ACCOUNT_TRADE_TYPE_MINUS;
        consumePage = this.userAccountService.getUserAccountDetailPage(consumePage, userId, tradeType);
        return Result.ok(consumePage);
    }
}

